package com.zhiyou100.taxi.acction;


import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.taxi.entity.Customer;
import com.zhiyou100.taxi.service.CustomerService;

import com.zhiyou100.taxi.service.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.plaf.multi.MultiMenuItemUI;
import java.util.List;

/**
 * @author zhaokuii11@163.com
 * @create 2021-11-23 21:48
 * @Description
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    CustomerService customerService;

    @RequestMapping("/show.action")
    public String show(Integer pageNum, HttpSession session) {
        System.out.println("查看数据");
        if (pageNum == null) {
            pageNum = 1;
        }
        System.out.println(pageNum + "《《==================");
//        List<Customer> list = customerService.getAll();
        PageInfo<Customer> customerPageInfo = customerService.getAllPage(pageNum);
        session.setAttribute("customerPageInfo", customerPageInfo);
        return "customList";
    }

    /**
     * 保存客户
     * 想查询是否有这个客户，如果有提示用户
     * 如果没有，查询Oracle 自增主键，保存数据
     *
     * @param customer
     */
    @RequestMapping("/add.action")
    public String add(@RequestBody Customer customer, HttpServletRequest request) {
        //查询用户是否已经存在
        Customer temp = customerService.getCustomerByPid(customer.getCustomerPid());
        String message = "客户已经存在";
        if (temp == null) {
            try {
                //数据库查询主键赋值插入
                String primaryKey = customerService.getPrimaryKey();
                customer.setCustomerId(primaryKey);
                //保存数据
                customerService.saveCustomer(customer);
                message = "插入成功";
            } catch (Exception e) {
                message = "插入失败";
                e.printStackTrace();
            }
        }
        System.out.println("message :: " + message);
        request.setAttribute("message", message);
        return "forward:/customer/show.action";
    }

    @RequestMapping("/deleteOne.action")
    public String deleteOne(String customerId, HttpServletRequest request) {
        System.out.println(customerId);
        int count = customerService.deletePrimaryKey(customerId);
        String message = "删除成功";
        if (count > 0) {
            message = "删除失败";
        }
        request.setAttribute("message", message);
        return "forward:/customer/show.action";
    }


    @ResponseBody
    @RequestMapping("/getOne.action")
    public Object getPriamary(@RequestParam("customerId") String customerId, HttpServletRequest request) {
        Customer customer = customerService.getCustomerByPrimaryKey(customerId);
//        request.setAttribute("customer", customer);
//        return "forward:/customer/show.action";
        return customer;
    }


    @RequestMapping("/update.action")
    public String updateOne(@RequestBody Customer customer, HttpServletRequest request) {
        System.out.println("修改后的数据====》" + customer);
        int len = customerService.updateOne(customer);
        if (len > 0) {
            request.setAttribute("message", "更新成功");
        } else {
            request.setAttribute("message", "更新失败");
        }
        return "forward:/customer/show.action";
    }

    @Autowired
    DictionaryService dictionaryService;

    @ResponseBody
    @RequestMapping("/state.action")
    public Object ajaxGetState() {
        return dictionaryService.getAllState();
    }

    @ResponseBody
    @RequestMapping("/city.action")
    public Object ajaxGetCity(String state) {
        return dictionaryService.getAllCity(state);
    }

    /**
     * 读取数据库，数据以 excel的形式给客户端下载
     *
     * @param request
     * @return
     */
    @RequestMapping("/excel/create.actin")
    public ResponseEntity<byte[]> createExcel(HttpServletRequest request) {

        //查询数据库
        //创建 excel文件 使用工具类
        //把 excel以附件的形式响应给客户端。
        ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(null, null, null);
        return responseEntity;
    }

    /**
     * 读取 excel文件，数据库写入数据
     * （不需要创建临文件直接传入流）
     * @param excel
     * @param request
     * @return
     */
    @RequestMapping("/excel/read.action")
    public String readExcel(@RequestParam("excel") MultipartFile excel, HttpServletRequest request) {

        return "";
    }

}
